#include "listeclient.h"
////////////////////////
//
//  Constructeur
//
////////////////////////
ListeClient::ListeClient()
{
}
////////////////////////
//
//  Fonction
//
////////////////////////

QStringList ListeClient::getListeVille()const
{
    QStringList list;
    for (int i = 0; i < count() - 1; i++)
    {
        if (!list.contains(at(i)->getAdresse().getVille()))
            list << at(i)->getAdresse().getVille();
    }
    list.sort();
    return list;
}
QStringList ListeClient::getListeDepartement()const
{
    QStringList list;
    for (int i = 0; i < count() - 1; i++)
    {
        if (!list.contains(at(i)->getAdresse().getDepartement()))
            list << at(i)->getAdresse().getDepartement();
    }
    list.sort();
    return list;
}
QStringList ListeClient::getTypeClient()const
{
    QStringList list;
    for (int i = 0; i < count() - 1; i++)
    {
        if (!list.contains(at(i)->getTypeClient()))
            list << at(i)->getTypeClient();
    }
    list.sort();
    return list;
}
QList<QDate> ListeClient::getListeParDateProgrammation()const
{

    QList<QDate> list;
    for (int i = 0; i < count() - 1; i++)
    {
        if (!list.contains(at(i)->getDateProgrammation()))
            list << at(i)->getDateProgrammation();
    }

    return list;
}
ListeClient ListeClient::getClientDateProgrammation(QDate dateProgrammation)const
{
    ListeClient list;
    for (int i = 0; i < count() - 1; i++)
    {
        if (at(i)->getDateProgrammation().month() == dateProgrammation.month())
            list << at(i);
    }
    return list;
}
ListeClient ListeClient::getClientDateContacter(QDate dateContact)const
{
    ListeClient list;
    for (int i = 0; i < count() - 1; i++)
    {
        for(int j = 0; j < at(i)->getListDateContact().count() - 1; j++)
        {
            if (at(i)->getListDateContact().at(j).getDate().dayOfYear() == dateContact.dayOfYear())
            {
                if (!list.contains(at(i)))
                    list << at(i);
            }
        }

    }
    return list;
}
ListeClient ListeClient::getClientDateContacterDansLaSemaine(QDate dateContact)const
{
    ListeClient list;
    for (int i = 0; i < count() - 1; i++)
    {
        for(int j = 0; j < at(i)->getListDateContact().count() - 1; j++)
        {
            if (at(i)->getListDateContact().at(j).getDate().weekNumber() == dateContact.weekNumber())
            {
                if (!list.contains(at(i)))
                    list << at(i);
            }
        }

    }
    return list;
}
Client* ListeClient::getById(int id)
{
    Client *c;
    foreach(c, *this)
    {
        if (c->getId() == id)
            return c;
    }
    return c;
}
void ListeClient::AddListeContact(ListeContact liste)
{
    Contact c;
    int i;
    bool condition;
    foreach(c , liste)
    {
        i = 0;
        condition = true;
        while(condition && i < count())
        {
            if (c.getClientId() == at(i)->getId())
            {
                at(i)->addContact(c);
                condition = true;
            }
            i++;
        }
    }
}
void ListeClient::AddListeDateContact(ListeDateContact liste)
{
    DateContact c;
    int i;
    bool condition;
//    qDebug() << "ajout date contact liste count " << liste.count();
    foreach(c , liste)
    {
//        qDebug() << "ajout date contact " << c.getClientId() << " - " << c.getContact().NomEtPrenom();
        i = 0;
        condition = true;
        while(condition && i < count())
        {
            if (c.getClientId() == at(i)->getId())
            {
//                qDebug() << "ajout date contact " << c.getContact().NomEtPrenom();
                at(i)->addDateContact(c);
                condition = true;
            }
            i++;
        }
    }
}

void ListeClient::AddListeDateObtenu(QList<DateObtenu> liste)
{
    DateObtenu c;
    int i;
    bool condition;
    foreach(c , liste)
    {
        i = 0;
        condition = true;
        while(condition && i < count())
        {
            if (c.getClientId() == at(i)->getId())
            {
                at(i)->addDateObtenu(c);
                condition = true;
            }
            i++;
        }
    }
}
void ListeClient::setListeClient(QSqlQuery query)
{
    while(query.next())
    {

        Client *client = new Client(query);
        this->append(client);
    }
}
